package org.feerepository.client;

import com.google.gwt.user.client.ui.TreeItem;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.feerepository.client.components.IndexedTreeItem;

/**
 *
 * @author Oleg Bulavitchi
 */
public class AppEventListerImpl implements AppEventListener
{
    List<EventListener> listeners = new ArrayList<EventListener>();
    private static final AppEventListerImpl appListener = new AppEventListerImpl();

    public static AppEventListerImpl getInstance()
    {
        return  appListener;
    }

    public void mainMenuItemSelected(TreeItem selectedItem)
    {
        HashMap<String, Object> results = new HashMap<String,Object>();
        results.put("SELECTED_JURISDICTION", selectedItem.getText());
        results.put("SELECTED_JURISDICTION_ID", ((IndexedTreeItem)selectedItem).getId());
        fireEvents(results);
        //fireEvents("SELECTED_JURISDICTION", selectedItem.getText());
        
    }

    private void fireEvents(HashMap <String, Object> properties)
    {
        for(EventListener e: listeners)
        {
            e.progpertiesChanged(properties);
        }
    }
    
    private void fireEvents(String name, Object value)
    {
        for(EventListener e: listeners)
        {
            e.propertyChanged(name, value);
        }
    }

    public void addApplicationEventListener(EventListener e)
    {
        listeners.add(e);
    }
}
